/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.foundation.chart.util;

import com.intelligent.ispc.common.utils.MathUtils;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.ui.LengthAdjustmentType;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;

import java.awt.*;

/**
 * Created by cherry on 2016/8/19.
 */
public class ChartUtil {

    /**
     * Draw warning lines.
     *
     * @param value
     * @param labelTitle
     * @param paintColor
     * @return
     */
    public static ValueMarker drawWarningLines(double value, String labelTitle, Color paintColor) {
        ValueMarker valuemarker = new ValueMarker(value);
        valuemarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        valuemarker.setPaint(paintColor);
        valuemarker.setStroke(new BasicStroke(0.3F));
        valuemarker.setLabel(labelTitle);
        valuemarker.setLabelFont(new Font("SansSerif", 0, 9));
        valuemarker.setLabelPaint(paintColor);
        valuemarker.setLabelAnchor(RectangleAnchor.CENTER);
        valuemarker.setLabelTextAnchor(TextAnchor.BOTTOM_CENTER);
        return valuemarker;
    }

    public static void adjustNumberAxis(XYPlot xyPlot, double[] data) {
        NumberAxis yAxis = (NumberAxis) xyPlot.getRangeAxis();
        double min = MathUtils.findMin(data);
        double max = MathUtils.findMax(data);
        double reserve = (max - min) * 0.20;
        yAxis.setRange(min - reserve, max + reserve);
    }

    public static void adjustNumberAxis(XYPlot xyPlot, double[] data, double[] y) {
        NumberAxis yAxis = (NumberAxis) xyPlot.getRangeAxis();

        double minData = MathUtils.findMin(data);
        double minY = MathUtils.findMin(y);
        double min = minData < minY ? minData : minY;

        double maxData = MathUtils.findMax(data);
        double maxY = MathUtils.findMax(y);
        double max = maxData < maxY ? maxY : maxData;

        double reserve = (max - min) * 0.20;
        if (max != min) {
            yAxis.setRange(min - reserve, max + reserve);
        }
    }
}
